import 'package:flutter_syt/utils/web_socket_utility.dart';

class MessageModel {
  int? type;
  int? msgType;
  String? content;
  String? picUrl;
  String? videoImagePath;
  String? customerName;
  String? source;
  List<GoodsMessage>? goods;
  SendStatus status;
  MessageDirection direction;
  int? createTime;
  String? authorization;
  String? lang;
  int? id;
  bool isPlay;

  // GoodsInfoContentModel? goodsModel;

//   0:"id" -> 460
// 1:"type" -> 10
// 2:"msg_type" -> 1
// 3:"content" -> "欢迎语，你好！很高兴为您服务！"
// 4:"pic_url" -> ""
// 5:"data" -> Map (3 items)
// 6:"create_time" -> 1651929127
// 7:"send_user_type" -> 1

  MessageModel(
      {this.type,
      this.msgType,
      this.content,
      this.picUrl,
      this.customerName,
      this.goods,
      this.authorization,
      this.lang,
      this.createTime,
      this.id,
      this.source,
      this.isPlay = false,
      this.videoImagePath,
      // this.direction,
      // this.goodsModel,
      this.status = SendStatus.SendStatusSendDone,
      this.direction = MessageDirection.RECEIVE});

  factory MessageModel.fromJson(Map<String, dynamic> json) => MessageModel(
        type: json['type'] == null
            ? null
            : json['type'] is String
                ? int.tryParse(json['type'])
                : json['type'].toInt(),
        msgType: json['msg_type'] == null
            ? null
            : json['msg_type'] is String
                ? int.tryParse(json['msg_type'])
                : json['msg_type'].toInt(),
        content: json['content'] as String?,
        picUrl: json['pic_url'] as String?,
        source: json['source'] as String?,
        direction: json['send_user_type'] == 1
            ? MessageDirection.RECEIVE
            : MessageDirection.SEND,
        customerName: json['customer_name'] as String?,
        authorization: json['Authorization'] as String?,
        id: json['id'] as int?,
        createTime: json['create_time'] as int?,
        lang: json['lang'] as String?,
        goods: (json['goods'] as List<dynamic>?)
            ?.map((e) => GoodsMessage.fromJson(e as Map<String, dynamic>))
            .toList(),
        // goodsModel: (json['type'] == 10 || json['type'] == '10')
        //     ? GoodsInfoContentModel.fromJson(json['content'])
        //     : null
      );

  Map<String, dynamic> toJson() => {
        'type': type,
        'msg_type': msgType,
        'content': content,
        'source': source,
        'pic_url': picUrl,
        'Authorization': authorization,
        'lang': lang,
        'customer_name': customerName,
        'goods': goods?.map((e) => e.toJson()).toList(),
      };
}

class GoodsMessage {
  int? id;
  int? attrId;

  GoodsMessage({this.id, this.attrId});

  factory GoodsMessage.fromJson(Map<String, dynamic> json) => GoodsMessage(
        id: json['id'] as int?,
        attrId: json['attr_id'] as int?,
      );

  Map<String, dynamic> toJson() => {
        'id': id,
        'attr_id': attrId,
      };
}

class GoodsInfoContentModel {
  String? type;
  String? name;
  String? picUrl;
  String? price;
  String? id;

  GoodsInfoContentModel(
      {this.type, this.name, this.picUrl, this.price, this.id});

  factory GoodsInfoContentModel.fromJson(Map<String, dynamic> json) {
    return GoodsInfoContentModel(
      id: json['id'] as String?,
      type: json['type'] as String?,
      name: json['name'] as String?,
      picUrl: json['pic_url'] as String?,
      price: json['price'] as String?,
    );
  }

  Map<String, dynamic> toJson() => {
        'type': type,
        'id': id,
        'pic_url': picUrl,
        'name': name,
        'price': price,
      };
}
